import java.awt.Color;
import java.awt.Cursor;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;

import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLProfile;
import javax.swing.JFrame;

import com.jogamp.opengl.util.FPSAnimator;

import edu.umd.fosite.physics.Simulation;
import edu.umd.fosite.ui.UIUtils;
import edu.umd.fosite.util.LoopTask;

public class Fosite {
	
	public static String version = "0.0.1";
	
	public static void main(String[] args) {
		JFrame frame = new JFrame("Fosite " + version);
		frame.setResizable(false);
		
		// Set a blank cursor
		BufferedImage cursorImg = new BufferedImage(16, 16,
				BufferedImage.TYPE_INT_ARGB);
		Cursor blankCursor = Toolkit.getDefaultToolkit()
				.createCustomCursor(cursorImg, new Point(0, 0), "blank cursor");
		frame.getContentPane().setCursor(blankCursor);
		
		GLCapabilities capabilities = new GLCapabilities(
				GLProfile.get(GLProfile.GL2));
		capabilities.setDoubleBuffered(true);
		World canvas = new World(capabilities, 129 * 2 - 1); // creates a
																      // world

		frame.add(canvas);
		InputHandler handler = new InputHandler(frame, canvas);
		handler.attach(frame);

		// Get screen size, and size frame appropriately
		int sizeX = Toolkit.getDefaultToolkit().getScreenSize().width  * 3 / 4;
		int sizeY = Toolkit.getDefaultToolkit().getScreenSize().height * 3 / 4;

		frame.setSize(sizeX, sizeY);
		frame.setBackground(Color.white);

		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});

		// Display
		UIUtils.centerOnScreen(frame);
		frame.setVisible(true);
		
		// Begin animation
		FPSAnimator animator = new FPSAnimator(canvas, 60);
		animator.start();
		
		// Begin simulation
		Simulation simulation = new Simulation();
		Thread simulationThread = new Thread(new LoopTask(simulation, 5));
		simulationThread.start();
	}

}
